//axios拦截器
import axios from "axios";
const qs = require("qs");
const http = axios;
http.defaults.timeout = 10000;
http.defaults.withCredentials = true;
// 请求拦截器
http.interceptors.request.use(
  config => {
    // get信息常传KEY值
    const key ={
      key:"4c5d0d5adfc71807503dd5ba0900062d"
    }
    // config["params"] =Object.assign({},key,config["params"]);
    if(config.method=="post"){
    if(config.data!={}&&config.data.qs==true){
    config.data = qs.stringify( config.data)
    }
    }
    //发送请求操作，统一再请求里加上userId
    // config.headers["userId"] = window.sessionStorage.getItem("userId");
    return config;
  },
  error => {
    //发送请求错误操作
    console.log("请求失败");
    return Promise.reject(error);
  }
);
// 响应拦截
http.interceptors.response.use(
  response => {
    // 200成功返回结果
    if(response.status==200){
      let res = Promise.resolve(response["data"])
      return res;
    }
  },
  error => {
    //对响应数据错误做操作
    console.log("请求error", error.message);
    return Promise.reject(error);
  }
);
export default http;